package TP6;


import anaydis.search.SymbolTable;

import java.util.ArrayList;
import java.util.Set;

/**
 * Created by IntelliJ IDEA.
 * User: Nicolás Marzano
 * Date: 9/11/11
 * Time: 20:07
 * To change this template use File | Settings | File Templates.
 */
abstract class AbstractSearchArray<K, V> implements SymbolTable<K, V>{
    public ArrayList<Entries<K, V>> entryList;
    private int size;

    public int size(){
        return size;
    }

    public boolean isEmpty(){
        return (size == 0);
    }

    public boolean containsKey(final K key){
        return search(key)!=-1;
    }

    public V get(final K key){
        if(search(key) != -1){
            return (entryList.get(search(key)).getValue());
        }
        return null;
    }

    public V put(final K key, final V value){
        if(search(key)!=-1){
            return (V)(entryList.get(search(key)).setValue(value));
        }
        for(int j=0; j<size; j++){
            int h = entryList.get(j).getKey().hashCode();
            if(h > key.hashCode()){
                entryList.add(j, new Entries(key, value));
                size++;
                return null;
            }
        }
        entryList.add(new Entries(key, value));
        size++;
        return null;
    }

    public void clear(){
        entryList = new ArrayList<Entries<K, V>>();
        size = 0;
    }

    public Set<Entry<K, V>> entrySet(){
        return null; // ??
    }

    public int search(Object o){
        return search(o, 0, size - 1);
    }

    abstract int search(Object o, int i, int j);

}

